<?php
/**
*
* @package phpBB3
* @version $Id$
* @copyright (c) 2005 phpBB Group
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

/**
*/

/**
* @ignore
*/
 
define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include_once($phpbb_root_path . 'common.' . $phpEx);
include_once($phpbb_root_path . 'includes/functions_display.' . $phpEx);
include_once($phpbb_root_path . 'sv_common.' . $phpEx);
include_once($phpbb_root_path . 'ofc/php-ofc-library/open-flash-chart.' . $phpEx);

// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup('stockvirtual');

$money = $user->data['user_money'];
$user_id 	= $user->data['user_id'];

if ($user_id == ANONYMOUS)
{
	login_box();
}

// Show main data.
draw_trading_data();

draw_visiting_user();

draw_registering_user();

list_active_user();

list_personal_setting();

list_query_user_action();

list_news_reading();

display_forums('', $config['load_moderators']);

// include nav links.
include_nav_links();

// Assign index specific vars
$template->assign_vars(array(

	'FORUM_IMG'				=> $user->img('forum_read', 'NO_NEW_POSTS'),
	'FORUM_NEW_IMG'			=> $user->img('forum_unread', 'NEW_POSTS'),
	'FORUM_LOCKED_IMG'		=> $user->img('forum_read_locked', 'NO_NEW_POSTS_LOCKED'),
	'FORUM_NEW_LOCKED_IMG'	=> $user->img('forum_unread_locked', 'NO_NEW_POSTS_LOCKED'),

	'S_LOGIN_ACTION'			=> append_sid("{$phpbb_root_path}ucp.$phpEx", 'mode=login'),
	'S_DISPLAY_BIRTHDAY_LIST'	=> ($config['load_birthdays']) ? true : false,
	'U_MONEY'					=> number_format($money),
	'S_LIST'					=> $list,
	'JASON_STRING'				=> $jason_string,
	'VISITING_USER_JASON_STRING'=> $visiting_user_jason_string,
	'REGISTERING_USER_JASON_STRING'=> $registering_user_jason_string,
	
	'S_QUERY_USER'				=> append_sid("{$phpbb_root_path}query_user.$phpEx", 'mode=query_user'),
	
	'U_MARK_FORUMS'		=> ($user->data['is_registered'] || $config['load_anon_lastread']) ? append_sid("{$phpbb_root_path}index.$phpEx", 'hash=' . generate_link_hash('global') . '&amp;mark=forums') : '',
	'U_MCP'				=> ($auth->acl_get('m_') || $auth->acl_getf_global('m_')) ? append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=main&amp;mode=front', true, $user->session_id) : '')
);

// Output page
page_header($user->lang['SV_HISTORY']);

$template->set_filenames(array(
	'body' => 'statistics.html')
);

page_footer();


function list_personal_setting()
{
	global $template, $db;
	
	$sql 	= 'SELECT COUNT(user_id) as num FROM sv_personal_setting WHERE allow_ad = 0';
	$result = $db->sql_query($sql); 
	$row = $db->sql_fetchrow($result);
	$not_allow_ad = $row['num'];	
	$db->sql_freeresult($result);
	
	$sql 	= 'SELECT COUNT(user_id) as num FROM sv_personal_setting WHERE allow_ad = 1';
	$result = $db->sql_query($sql); 
	$row = $db->sql_fetchrow($result);
	$allow_ad = $row['num'];	
	$db->sql_freeresult($result);
	
	$template->assign_vars(array(
		'NUM_USER_NOT_ALLOW_AD'					=> $not_allow_ad,
		'NUM_USER_ALLOW_AD'						=> $allow_ad,
	));
	
	$sql 	= 'SELECT COUNT(user_id) as num FROM sv_personal_setting WHERE news_max_number = 0';
	$result = $db->sql_query($sql); 
	$row = $db->sql_fetchrow($result);
	$news_max_0 = $row['num'];	
	$db->sql_freeresult($result);
	
	$sql 	= 'SELECT COUNT(user_id) as num FROM sv_personal_setting WHERE news_max_number = 5';
	$result = $db->sql_query($sql); 
	$row = $db->sql_fetchrow($result);
	$news_max_5 = $row['num'];	
	$db->sql_freeresult($result);
	
	$sql 	= 'SELECT COUNT(user_id) as num FROM sv_personal_setting WHERE news_max_number = 10';
	$result = $db->sql_query($sql); 
	$row = $db->sql_fetchrow($result);
	$news_max_10 = $row['num'];	
	$db->sql_freeresult($result);
	
	$sql 	= 'SELECT COUNT(user_id) as num FROM sv_personal_setting WHERE news_max_number = 20';
	$result = $db->sql_query($sql); 
	$row = $db->sql_fetchrow($result);
	$news_max_20 = $row['num'];	
	$db->sql_freeresult($result);
	
	$template->assign_vars(array(
		'NUM_USER_NEWS_MAX_0'						=> $news_max_0,
		'NUM_USER_NEWS_MAX_5'						=> $news_max_5,
		'NUM_USER_NEWS_MAX_10'						=> $news_max_10,
		'NUM_USER_NEWS_MAX_20'						=> $news_max_20,
	));
	
	$sql 	= 'SELECT COUNT(user_id) as num FROM sv_personal_setting WHERE market_info = 0';
	$result = $db->sql_query($sql); 
	$row = $db->sql_fetchrow($result);
	$market_info_0 = $row['num'];	
	$db->sql_freeresult($result);
	
	$sql 	= 'SELECT COUNT(user_id) as num FROM sv_personal_setting WHERE market_info = 1';
	$result = $db->sql_query($sql); 
	$row = $db->sql_fetchrow($result);
	$market_info_1 = $row['num'];	
	$db->sql_freeresult($result);
	
	$sql 	= 'SELECT COUNT(user_id) as num FROM sv_personal_setting WHERE market_info = 2';
	$result = $db->sql_query($sql); 
	$row = $db->sql_fetchrow($result);
	$market_info_2 = $row['num'];	
	$db->sql_freeresult($result);
	
	$template->assign_vars(array(
		'NUM_USER_MARKET_INFO_0'						=> $market_info_0,
		'NUM_USER_MARKET_INFO_1'						=> $market_info_1,
		'NUM_USER_MARKET_INFO_2'						=> $market_info_2,
	));
	
	$sql 	= 'SELECT COUNT(user_id) as num FROM sv_personal_setting WHERE delegation_list_info = 0';
	$result = $db->sql_query($sql); 
	$row = $db->sql_fetchrow($result);
	$delegation_list_info_0 = $row['num'];	
	$db->sql_freeresult($result);
	
	$sql 	= 'SELECT COUNT(user_id) as num FROM sv_personal_setting WHERE delegation_list_info = 1';
	$result = $db->sql_query($sql); 
	$row = $db->sql_fetchrow($result);
	$delegation_list_info_1 = $row['num'];	
	$db->sql_freeresult($result);
	
	$template->assign_vars(array(
		'NUM_USER_DELEGATION_LIST_INFO_0'						=> $delegation_list_info_0,
		'NUM_USER_DELEGATION_LIST_INFO_1'						=> $delegation_list_info_1,
	));
	
	$sql 	= 'SELECT COUNT(user_id) as num FROM sv_personal_setting WHERE LENGTH(belief) = 0';
	$result = $db->sql_query($sql); 
	$row = $db->sql_fetchrow($result);
	$empty_belief = $row['num'];	
	$db->sql_freeresult($result);
	
	$sql 	= 'SELECT COUNT(user_id) as num FROM sv_personal_setting WHERE LENGTH(belief) != 0';
	$result = $db->sql_query($sql); 
	$row = $db->sql_fetchrow($result);
	$with_belief = $row['num'];	
	$db->sql_freeresult($result);
	
	$template->assign_vars(array(
		'NUM_USER_WITH_EMPTY_BELIEF'						=> $empty_belief,
		'NUM_USER_WITH_BELIEF'						=> $with_belief,
	));
}

function list_active_user()
{
	global $template;
	
	$daily = get_cache_info('daily_active_users');
	$weekly = get_cache_info('weekly_active_users');
	$monthly = get_cache_info('monthly_active_users');
		
	$daily_users 		= explode(',', $daily);
	$weekly_users		= array();
	$monthly_users		= array();
	
	$tmp_weekly_users 	= explode(',', $weekly);	
	$tmp_monthly_users 	= explode(',', $monthly);
	
	foreach($tmp_weekly_users as $w)
	{
		if (in_array($w, $daily_users))
		{
			continue;	
		}
		
		$weekly_users[] = $w;
	}
	
	foreach($tmp_monthly_users as $m)
	{
		if (in_array($m, $daily_users) || in_array($m, $weekly_users))
		{
			continue;	
		}
		
		$monthly_users[] = $m;
	}

/*	
	echo "daily: <BR>" . PHP_EOL;	
	print_r($daily_users);
	echo "<BR> weekly: <BR>" . PHP_EOL;	
	print_r($weekly_users);
	echo "<BR> monthly: <BR>" . PHP_EOL;	
	print_r($monthly_users);
*/	
	
	assign_active_user_id($daily_users, 'daily_active_user_list');
	assign_active_user_id($weekly_users, 'weekly_active_user_list');
	assign_active_user_id($monthly_users, 'monthly_active_user_list');
		
	$template->assign_vars(array(

		'ACTIVE_DAILY_USER_NUM'					=> count($daily_users),
		'ACTIVE_WEEKLY_USER_NUM'				=> count($weekly_users),
		'ACTIVE_MONTHLY_USER_NUM'				=> count($monthly_users),
	));

}

function assign_active_user_id($group, $str)
{
	global $db, $template;
	
	foreach($group as $id)
	{	
		if ($id < 50)
			continue;
			
		$sql 	= 'SELECT allow_ad FROM sv_personal_setting WHERE user_id = ' . $id;
		$result = $db->sql_query($sql); 
		$row = $db->sql_fetchrow($result);
		$with_ad = $row['allow_ad'];
		$db->sql_freeresult($result);				
	
		$template->assign_block_vars($str, array(
			'USER_ID'					=> $id,
			'USERNAME'					=> get_username($id),
			'WITH_AD'					=> $with_ad,
			)
		);
	}
}

function list_news_reading()
{
	global $db, $template;
	
	$sql 	= 'SELECT * FROM sv_read_news_record ORDER BY time DESC';
	$result = $db->sql_query_limit($sql, 300);
	while ($row = $db->sql_fetchrow($result))
	{
		$user_id 	= $row['user_id'];
		$news_id	= $row['news_id'];
		$time 		= $row['time'];		
		
		$news_info	= get_news_info($news_id);
			
		$template->assign_block_vars('news_reading_list', array(
			'USER_ID'						=> $user_id,
			'USERNAME'						=> get_username($user_id),
			'TIME'							=> get_time_string((int)$time),			
			'NEWS_TITLE'					=> $news_info['title'],
			'NEWS_LINK'						=> $news_info['link'],
			'NEWS_SNIPPET'					=> $news_info['snippet'],
			)
		);
	}
}

function list_query_user_action()
{
	global $db, $template;
	
	$sql 	= 'SELECT * FROM sv_query_user_record ORDER BY time DESC';
	$result = $db->sql_query_limit($sql, 300);
	while ($row = $db->sql_fetchrow($result))
	{
		$queryer_id = $row['user_id'];
		$queryee_id = $row['query_user_id'];
		$time 		= $row['time'];
		
		// skip bots.
		if ($queryer_id < 50)
			continue;
			
		$template->assign_block_vars('query_user_list', array(
			'QUERYER_ID'						=> $queryer_id,
			'QUERYER_USERNAME'					=> get_username($queryer_id),
			'QUERYEE_ID'						=> $queryee_id,
			'QUERYEE_USERNAME'					=> get_username($queryee_id),
			'TIME'								=> get_time_string((int)$time),			
			)
		);
	}
}

function draw_registering_user()
{
	global $registering_user_jason_string, $db;

	$date			= array();
	$daily_value	= array();
	$tmp_date		= array();
	
	$max			= 0;
	$min			= 100000000000; // just pick a number big enough for now. 
	$num			= 0;
		
	$sql 	= 'SELECT user_regdate FROM phpbb_users';
	$result = $db->sql_query($sql);
	while ($row = $db->sql_fetchrow($result))
	{
		$d	= get_date_from_time($row['user_regdate']);
		
		$tmp_date[$d] = $tmp_date[$d] + 1;		
	}
	
	ksort($tmp_date);
		
	foreach ($tmp_date as $key => $value)
	{
		$date[] 		= get_date_string($key);
		$daily_value[] 	= $value;
		
		if ($value > $max)
			$max = $value;
		if ($value < $min)
			$min = $value;			
	}
		
	// prepare open-flash-chart 
	$chart = new open_flash_chart();
	
	$title = new title('');
	$chart->set_title( $title );
	
	// set dot style for daily user number.
	$user_num_dot = new dot();
	$user_num_dot->size(3)->tooltip('#x_label#<br>Daily Registering Users:#val#');
		
	// set line style for daily user number.
	$user_num_line	= new line();
	$user_num_line->set_colour( '#BFAFAF');
	$user_num_line->set_values( $daily_value );	
	$user_num_line->set_default_dot_style( $user_num_dot );		
	$chart->add_element( $user_num_line );

	
	// set X labels.
	$x_labels = new x_axis_labels();
	$x_labels->set_steps( 10 );
	$x_labels->set_vertical();
	$x_labels->set_colour( '#A2ACBA' );
	$x_labels->set_labels( $date );
	
	$x = new x_axis();
	$x->set_colour( '#A2ACBA' );
	$x->set_grid_colour( '#D7E4A3' );
	$x->set_offset( false );
	$x->set_steps(1);
	
	// Add the X Axis Labels to the X Axis
	$x->set_labels( $x_labels ); 
	
	$chart->set_x_axis( $x );
	
	$y = new y_axis();
	$y->set_range( $min, $max, (int)(($max- $min)/5) ); //min, max, interval
	$chart->set_y_axis( $y );
			
	$registering_user_jason_string = $chart->toPrettyString();	
}

function draw_visiting_user()
{
	global $visiting_user_jason_string, $db;

	$date			= array();
	$daily_value	= array();
	$weekly_value	= array();
	$monthly_value	= array();
	$max			= 0;
	$min			= 100000000000; // just pick a number big enough for now. 
	$num			= 0;
		
	$sql 	= 'SELECT * FROM sv_visiting_user';
	$result = $db->sql_query($sql);
	while ($row = $db->sql_fetchrow($result))
	{
		$date[]				= get_date_string($row['date']);
		$daily_value[]		= $daily		= (int)$row['daily_user_num'];
		$weekly_value[]		= $weekly		= (int)$row['weekly_user_num'];
		$monthly_value[]	= $monthly		= (int)$row['monthly_user_num'];
				
		// update the max/min of transaction number.
		if ($max < $monthly)
			$max = $monthly;
		if ($min > $daily)
			$min = $daily;	
		$num += 1;
	}
		
	// prepare open-flash-chart 
	$chart = new open_flash_chart();
	
	$title = new title('');
	$chart->set_title( $title );
	
	// set dot style for daily user number.
	$user_num_dot = new dot();
	$user_num_dot->size(3)->tooltip('#x_label#<br>Daily Users:#val#');
		
	// set line style for daily user number.
	$user_num_line	= new line();
	$user_num_line->set_colour( '#BFAFAF');
	$user_num_line->set_values( $daily_value );	
	$user_num_line->set_default_dot_style( $user_num_dot );		
	$chart->add_element( $user_num_line );

	// set dot style for weekly user number.
	$weekly_user_num_dot = new dot();
	$weekly_user_num_dot->size(3)->tooltip('#x_label#<br>Weekly Users:#val#');
		
	// set line style for weekly user number.
	$weekly_user_num_line	= new line();
	$weekly_user_num_line->set_colour( '#7FAFAF');
	$weekly_user_num_line->set_values( $weekly_value );	
	$weekly_user_num_line->set_default_dot_style( $weekly_user_num_dot );		
	$chart->add_element( $weekly_user_num_line );
	
	// set dot style for monthly user number.
	$monthly_user_num_dot = new dot();
	$monthly_user_num_dot->size(3)->tooltip('#x_label#<br>monthly Users:#val#');
		
	// set line style for monthly user number.
	$monthly_user_num_line	= new line();
	$monthly_user_num_line->set_colour( '#BF6FAF');
	$monthly_user_num_line->set_values( $monthly_value );	
	$monthly_user_num_line->set_default_dot_style( $monthly_user_num_dot );		
	$chart->add_element( $monthly_user_num_line );	
	
	// set X labels.
	$x_labels = new x_axis_labels();
	$x_labels->set_steps( 10 );
	$x_labels->set_vertical();
	$x_labels->set_colour( '#A2ACBA' );
	$x_labels->set_labels( $date );
	
	$x = new x_axis();
	$x->set_colour( '#A2ACBA' );
	$x->set_grid_colour( '#D7E4A3' );
	$x->set_offset( false );
	$x->set_steps(1);
	
	// Add the X Axis Labels to the X Axis
	$x->set_labels( $x_labels ); 
	
	$chart->set_x_axis( $x );
	
	$y = new y_axis();
	$y->set_range( $min, $max, (int)(($max- $min)/5) ); //min, max, interval
	$chart->set_y_axis( $y );
			
	$visiting_user_jason_string = $chart->toPrettyString();	
}

function draw_trading_data()
{
	global $jason_string, $db;
	
	$date			= array();
	$value			= array();
	$max			= 0;
	$min			= 100000000000; // just pick a number big enough for now. 
	$num			= 0;
	$tran_num 		= 0;
	
	$daily_user_num		= array();
	$daily_user_id		= array();
	$weekly_user_num	= array();
	$weekly_user_id		= array();
	$monthly_user_num	= array();
	$monthly_user_id	= array();
	$max_user		= 0;
	$min_user		= 1000000;
	
	$tran_date 	= '';
	
	for ($i = 0; $i < 7; ++$i)
	{
		$weekly_user_id[$i] = array();
	}
	for ($i = 0; $i < 30; ++$i)
	{
		$monthly_user_id[$i] = array();
	}
	
	$sql 	= 'SELECT * FROM sv_transaction ORDER BY request_time ASC';
	$result = $db->sql_query($sql);
	while ($row = $db->sql_fetchrow($result))
	{
		if (is_valid_corp_id($row['corp_id']) == false)
		{
			continue;
		}
		
		if ($tran_date == '')
		{
			$tran_date = get_date_from_time((int)$row['request_time']);
		}
		
		// update user id list for every time interval.
		$id = $row['user_id'];
		$daily_user_id[$id] = 1;
		
		for ($i = 0; $i < 7; ++$i)
		{
			$weekly_user_id[$i][$id] = 1;
		}
		for ($i = 0; $i < 30; ++$i)
		{
			$monthly_user_id[$i][$id] = 1;
		}
		
		// Check if we move on to the next day.
		if ($tran_date == get_date_from_time((int)$row['request_time']))
		{
			$tran_num += 1;
		}
		else
		{
			// transaction date changes, update values for the date. 
			$date[] 			= get_date_string($tran_date);
			$value[]			= $tran_num;
			$daily_user_num[]	= sizeof($daily_user_id);
			$weekly_user_num[]	= sizeof($weekly_user_id[$num%7]);
			$monthly_user_num[]	= sizeof($monthly_user_id[$num%30]);
			
			// update the max/min of user number.
			$temp_user_num = sizeof($monthly_user_id[$num%30]);
			if ($max_user < $temp_user_num)
				$max_user = $temp_user_num;
			$temp_user_num = sizeof($daily_user_id);
			if ($min_user > $temp_user_num)
				$min_user = $temp_user_num;
				
			// update the max/min of transaction number.
			if ($max < $tran_num)
				$max = $tran_num;
			if ($min > $tran_num)
				$min = $tran_num;
			
			// reset.
			while( sizeof($daily_user_id) !=0 )
			{
				array_pop($daily_user_id);
			}
			$tran_num = 0;
			$temp_user_num = 0;
			$tran_date = get_date_from_time((int)$row['request_time']);
			
			// reset weekly user number.
			while( sizeof($weekly_user_id[$num%7]) !=0 )
			{
				array_pop($weekly_user_id[$num%7]);
			}			
	
			// reset monthly user number.
			while( sizeof($monthly_user_id[$num%30]) !=0 )
			{
				array_pop($monthly_user_id[$num%30]);
			}			
			
			$num += 1;
		}	
			
	}
	
	// update the data on last day.
	$date[] 			= get_date_string($tran_date);
	$value[]			= $tran_num;
	$daily_user_num[]	= sizeof($daily_user_id);
	$weekly_user_num[]	= sizeof($weekly_user_id[$num%7]);
	$monthly_user_num[]	= sizeof($monthly_user_id[$num%30]);
			
	$num		+= 1;
	
	if ($max < $tran_num)
		$max = $tran_num;
	if ($min > $tran_num)
		$min = $tran_num;
	
	// prepare open-flash-chart 
	$chart = new open_flash_chart();
	
	$title = new title('');
	$chart->set_title( $title );
	
	$area = new area();
	$area->set_colour( '#5B56B6' );
	$area->set_values( $value );
	$chart->add_element( $area );
	
	// set dot style for daily user number.
	$user_num_dot = new dot();
	$user_num_dot->size(3)->tooltip('#x_label#<br>Daily Users:#val#');
		
	// set line style for daily user number.
	$user_num_line	= new line();
	$user_num_line->set_colour( '#BFAFAF');
	$user_num_line->set_values( $daily_user_num );	
	$user_num_line->set_default_dot_style( $user_num_dot );		
	$user_num_line->attach_to_right_y_axis();
	$chart->add_element( $user_num_line );
	
	// set dot style for weekly user number.
	$weekly_user_num_dot = new dot();
	$weekly_user_num_dot->size(3)->tooltip('#x_label#<br>Weekly Users:#val#');
		
	// set line style for weekly user number.
	$weekly_user_num_line	= new line();
	$weekly_user_num_line->set_colour( '#7FAFAF');
	$weekly_user_num_line->set_values( $weekly_user_num );	
	$weekly_user_num_line->set_default_dot_style( $weekly_user_num_dot );		
	$weekly_user_num_line->attach_to_right_y_axis();
	$chart->add_element( $weekly_user_num_line );
	
	// set dot style for weekly user number.
	$monthly_user_num_dot = new dot();
	$monthly_user_num_dot->size(3)->tooltip('#x_label#<br>monthly Users:#val#');
		
	// set line style for weekly user number.
	$monthly_user_num_line	= new line();
	$monthly_user_num_line->set_colour( '#BF6FAF');
	$monthly_user_num_line->set_values( $monthly_user_num );	
	$monthly_user_num_line->set_default_dot_style( $monthly_user_num_dot );		
	$monthly_user_num_line->attach_to_right_y_axis();
	$chart->add_element( $monthly_user_num_line );
		
	// set X labels.
	$x_labels = new x_axis_labels();
	$x_labels->set_steps( 10 );
	$x_labels->set_vertical();
	$x_labels->set_colour( '#A2ACBA' );
	$x_labels->set_labels( $date );
	
	$x = new x_axis();
	$x->set_colour( '#A2ACBA' );
	$x->set_grid_colour( '#D7E4A3' );
	$x->set_offset( false );
	$x->set_steps(4);
	
	// Add the X Axis Labels to the X Axis
	$x->set_labels( $x_labels ); 
	
	$chart->set_x_axis( $x );
	
	$y = new y_axis();
	$y->set_range( $min, $max, (int)(($max- $min)/5) ); //min, max, interval
	$chart->set_y_axis( $y );
	
	$y_right = new y_axis_right();
	$y_right->set_range( $min_user, $max_user, (int)(($max_user - $min_user)/10));
	$y_right->set_stroke( 3 );
	$y_right->set_colour( '#BFAFAF' );
	$y_right->set_tick_length( 12 );
	$y_right->set_steps( (int)(($max_user - $min_user)/10));
	$chart->set_y_axis_right( $y_right );
		
	$jason_string = $chart->toPrettyString();	
}

?>